package com.zz.inventory.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.security.annotation.InnerAuth;
import jakarta.servlet.http.HttpServletResponse;
import org.example.dto.SchoolPackageRecordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.inventory.domain.UserPackageRecords;
import com.zz.inventory.service.IUserPackageRecordsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 套餐权益记录Controller
 * 
 * @author zz
 * @date 2025-07-06
 */
@RestController
@RequestMapping("/records")
public class UserPackageRecordsController extends BaseController
{
    @Autowired
    private IUserPackageRecordsService userPackageRecordsService;


    /**
     * 新增套餐权益记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody SchoolPackageRecordDTO schoolPackageRecordDTO)
    {
        Date currentDate = new Date();
// 创建 Calendar 实例，并设置为当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
// 将时间增加 1 年（年份字段加 1）
        calendar.add(Calendar.YEAR, 1);
        UserPackageRecords user = new UserPackageRecords();
        user.setSchoolId(schoolPackageRecordDTO.getSchoolId()); //学校ID
        user.setPackageId(schoolPackageRecordDTO.getPackageId());  //套餐ID
        user.setStartTime(new Date());   //开始时间
        user.setEndTime(calendar.getTime());       //到期时间
        user.setBillId(schoolPackageRecordDTO.getBillId());       //账单ID
        user.setUserId(schoolPackageRecordDTO.getUserId());       //用户ID
        user.setStatus("生效中");
        throw new RuntimeException("测试 Seata 回滚"); // 主动抛异常：用于测试Seata分布式事务的回滚机制
       // return toAjax(userPackageRecordsService.insertUserPackageRecords(user));
    }

    /**
     * 查询套餐权益记录列表
     */
    @RequiresPermissions("schoolmanage:records:list")
    @GetMapping("/list")
    public TableDataInfo list(UserPackageRecords userPackageRecords)
    {
        startPage();
        List<UserPackageRecords> list = userPackageRecordsService.selectUserPackageRecordsList(userPackageRecords);
        return getDataTable(list);
    }

    /**
     * 导出套餐权益记录列表
     */
    @RequiresPermissions("schoolmanage:records:export")
    @Log(title = "套餐权益记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserPackageRecords userPackageRecords)
    {
        List<UserPackageRecords> list = userPackageRecordsService.selectUserPackageRecordsList(userPackageRecords);
        ExcelUtil<UserPackageRecords> util = new ExcelUtil<UserPackageRecords>(UserPackageRecords.class);
        util.exportExcel(response, list, "套餐权益记录数据");
    }

    /**
     * 获取套餐权益记录详细信息
     */
    @RequiresPermissions("schoolmanage:records:query")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(userPackageRecordsService.selectUserPackageRecordsByRecordId(recordId));
    }



    /**
     * 修改套餐权益记录
     */
    @RequiresPermissions("schoolmanage:records:edit")
    @Log(title = "套餐权益记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserPackageRecords userPackageRecords)
    {
        return toAjax(userPackageRecordsService.updateUserPackageRecords(userPackageRecords));
    }

    /**
     * 删除套餐权益记录
     */
    @RequiresPermissions("schoolmanage:records:remove")
    @Log(title = "套餐权益记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(userPackageRecordsService.deleteUserPackageRecordsByRecordIds(recordIds));
    }



    @InnerAuth
    @PostMapping("/compensate")
    public AjaxResult compensateRecord(@RequestBody UserPackageRecords dto) {

        return toAjax(userPackageRecordsService.insertUserPackageRecords(dto));
    }
}
